home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_78 / snddemo.c < prev    next >
Text File  |  1995-01-01  |  18KB  |  811 lines

  1. /*
  2. // SNDDEMO.C
  3. //
  4. // Demo program for the Sefx sound effects library
  5. //
  6. // Copyright (c) 1992 Bri Productions
  7. //
  8. */
  9.  
  10.  
  11. #include "sefx.h"
  12. #include <dos.h>
  13. #include <bios.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <conio.h>
  17. #include <ctype.h>
  18.  
  19.  
  20. /*
  21. //-------------------------------------
  22. //
  23. // Function macros
  24. //
  25. //-------------------------------------
  26. */
  27.  
  28. #define  fgetw(a)    getw(a)
  29. #define  fputw(a,b)  putw(a,b)
  30.  
  31. #define  INSERT      (((unsigned)*((unsigned far *)(0x400017l)) & 0x80) != 0)
  32.  
  33. #define  cursor(a)   _setcursortype(a)
  34.  
  35.  
  36. /*
  37. //-------------------------------------
  38. //
  39. // keystroke defines
  40. //
  41. //-------------------------------------
  42. */
  43.  
  44. #define  F1    0x3B00
  45. #define  F2    0x3C00
  46. #define  F3    0x3D00
  47. #define  F4    0x3E00
  48. #define  F5    0x3F00
  49. #define  F6    0x4000
  50. #define  F7    0x4100
  51. #define  F8    0x4200
  52. #define  F9    0x4300
  53. #define  F10   0x4400
  54. #define  ESC   0x011B
  55. #define  UP    0x4800
  56. #define  DOWN  0x5000
  57. #define  LEFT  0x4B00
  58. #define  RIGHT 0x4D00
  59. #define  TAB   0x0F09
  60. #define  STAB  0x0F00
  61. #define  HOME  0x4700
  62. #define  END   0x4F00
  63. #define  PGUP  0x4900
  64. #define  PGDN  0x5100
  65. #define  ENTER 0x1C0D
  66. #define  BS    0x0E08
  67. #define  DEL   0x5300
  68. #define  SPACE 0x3920
  69.  
  70.  
  71. /*
  72. //-------------------------------------
  73. //
  74. // screen colors
  75. //
  76. //-------------------------------------
  77. */
  78.  
  79. #define  VALUES   YELLOW|(LIGHTGRAY<<4)
  80. #define  TEXT     BLUE|(LIGHTGRAY<<4)
  81. #define  FIELD    YELLOW|(BLACK<<4)
  82. #define  INFO     BLACK|(CYAN<<4)
  83. #define  BORDER   DARKGRAY|(LIGHTGRAY<<4)
  84. #define  TITLE    RED|(LIGHTGRAY<<4)
  85.  
  86.  
  87. /*
  88. //-------------------------------------
  89. //
  90. // screen coordinates
  91. //
  92. //-------------------------------------
  93. */
  94.  
  95. #define LMAR      21
  96. #define TMAR      3
  97. #define HSPC      5
  98. #define VSPC      3
  99. #define COL       5
  100. #define SNDRES    72, TMAR + 9
  101. #define SNDHOLD   71, TMAR + 12
  102. #define SNDMS     LMAR + 9, 21
  103.  
  104.  
  105. /*
  106. //-------------------------------------
  107. //
  108. // Musical notes
  109. //
  110. //-------------------------------------
  111. */
  112.  
  113. #define  C     262
  114. #define  D     294
  115. #define  E     330
  116. #define  F     349
  117. #define  G     392
  118. #define  A     440
  119. #define  B     494
  120.  
  121.  
  122. /*
  123. //-------------------------------------
  124. //
  125. // misc constants
  126. //
  127. //-------------------------------------
  128. */
  129.  
  130. #define MAXFIELD  18
  131. #define LARGE     (_SOLIDCURSOR )
  132. #define SMALL     (_NORMALCURSOR)
  133.  
  134.  
  135. /*
  136. //-------------------------------------
  137. //
  138. // function prototype
  139. //
  140. //-------------------------------------
  141. */
  142.  
  143. void Init      (void);
  144. void Uninit    (void);
  145. void Highlight (struct field *_field, int on_off);
  146. void UpdateMs  (void);
  147. void delete    (char *del);
  148. void insert    (char c, char *ins);
  149. void blank     (char *blnk);
  150. int  space     (char *c);
  151.  
  152.  
  153. /*
  154. //-------------------------------------
  155. //
  156. // global variables
  157. //
  158. //-------------------------------------
  159. */
  160.  
  161. TONE scale[] = { { C, 12 },
  162.                  { D, 12 },
  163.                  { E, 12 },
  164.                  { F, 12 },
  165.                  { G, 12 },
  166.                  { A, 12 },
  167.                  { B, 12 },
  168.                  { C<<1, 12} };
  169.  
  170.  
  171. static struct field{
  172.    int value;           /* field value    */
  173.    char *format;        /* printf format  */
  174.    int row;             /* y coordinate   */
  175.    int col;             /* x coordinate   */
  176.    int next;            /* next field     */
  177.    int prev;            /* previous field */
  178.    int size;            /* field size     */
  179.    }field[MAXFIELD] = {
  180.       { 100, "%*u", TMAR + 0*VSPC, LMAR + 0 ,  1, 17, 4},  /* 0 SndTone    */
  181.       {   3, "%*u", TMAR + 0*VSPC, LMAR + 6 ,  2,  0, 3},
  182.       {  23, "%*u", TMAR + 1*VSPC, LMAR + 0 ,  3,  1, 3},  /* 2 SndPause   */
  183.       {   1, "%*u", TMAR + 2*VSPC, LMAR + 11,  4,  2, 3},  /* 3 SndString  */
  184.       { 100, "%*u", TMAR + 3*VSPC, LMAR + 0 ,  5,  3, 4},  /* 4 SndSlide   */
  185.       {  -3, "%*d", TMAR + 3*VSPC, LMAR + 6 ,  6,  4, 3},
  186.       {  18, "%*u", TMAR + 3*VSPC, LMAR + 11,  7,  5, 3},
  187.       {   1, "%*u", TMAR + 3*VSPC, LMAR + 16,  8,  6, 3},
  188.       {1400, "%*u", TMAR + 4*VSPC, LMAR + 0 ,  9,  7, 4},  /* 8 SndTrill   */
  189.       { 600, "%*u", TMAR + 4*VSPC, LMAR + 6 , 10,  8, 4},
  190.       {   1, "%*u", TMAR + 4*VSPC, LMAR + 12, 11,  9, 3},
  191.       {   1, "%*u", TMAR + 4*VSPC, LMAR + 17, 12, 10, 3},
  192.       {  18, "%*u", TMAR + 4*VSPC, LMAR + 22, 13, 11, 3},
  193.       {1100, "%*u", TMAR + 5*VSPC, LMAR + 0 , 14, 12, 4},  /* 13 SndPulse  */
  194.       {   2, "%*u", TMAR + 5*VSPC, LMAR + 6 , 15, 13, 3},
  195.       {   9, "%*u", TMAR + 5*VSPC, LMAR + 11, 16, 14, 3},
  196.       {   8, "%*u", TMAR + 5*VSPC, LMAR + 16, 17, 15, 3},
  197.       {1000, "%*u", TMAR + 6*VSPC, LMAR + 0 ,  0, 16, 5}   /* 17 SndMs     */
  198.    };
  199.  
  200.  
  201. int x, y;                     /* cursor location   */
  202. int tbase = 3;
  203. char *tbase_txt[] = { "ultra ",
  204.                       " high ",
  205.                       "medium",
  206.                       " low  "
  207.                     };
  208. int hold = OFF;
  209. char *hold_txt[2] = { "OFF",
  210.                       "ON "
  211.                       };
  212.  
  213.  
  214. /*
  215. //-------------------------------------
  216. //
  217. // main()
  218. //
  219. //-------------------------------------
  220. */
  221.  
  222. int main(void)
  223. {
  224. register struct field *_field = field;    /* current field pointer   */
  225. int pos = 0;                              /* cursor field location   */
  226. int key;                                  /* keystroke               */
  227. char buf[17];                             /* field buffer            */
  228. long len, old_len = 1;                    /* sound queue length      */
  229.  
  230.  
  231.       /* Call the initialization function and   */
  232.       /* initialize the field buffer            */
  233.  
  234.    Init();
  235.    sprintf(buf, _field->format, _field->size, _field->value);
  236.  
  237.  
  238.       /* Monitor and update, if necessary, the sound queue status */
  239.       /* and length while waiting for a keystroke.                */
  240.  
  241.    while(1)
  242.    {
  243.  
  244.       textattr(VALUES);
  245.       UpdateMs();
  246.       while(!bioskey(1))
  247.       {
  248.  
  249.             /* If the sound queue length has changed  */
  250.             /* update the screen.                     */
  251.  
  252.          len = SndLen();
  253.          if(len != old_len)
  254.          {
  255.             cursor(OFF);
  256.             gotoxy(72, TMAR);
  257.             cprintf("%-7lu", len);
  258.             old_len = len;
  259.          }
  260.  
  261.  
  262.             /* Restore the cursor before  */
  263.             /* processing the keystroke  */
  264.  
  265.          gotoxy(x, y);
  266.          if(INSERT)
  267.             cursor(LARGE);
  268.          else
  269.             cursor(SMALL);
  270.  
  271.       }
  272.       textattr(FIELD);
  273.  
  274.  
  275.          /* Save a copy of the keystroke and process it */
  276.  
  277.       cursor(OFF);
  278.       switch(key = bioskey(0))
  279.       {
  280.  
  281.          case ENTER:
  282.          case TAB:
  283.          case DOWN:
  284.             _field->value = atoi(buf);
  285.             Highlight(_field, OFF);
  286.             _field = field + _field->next;
  287.             sprintf(buf, _field->format, _field->size, _field->value);
  288.             Highlight(_field, ON);
  289.             pos = 0;
  290.             break;
  291.  
  292.  
  293.          case STAB:
  294.          case UP:
  295.             _field->value = atoi(buf);
  296.             Highlight(_field, OFF);
  297.             _field = field + _field->prev;
  298.             sprintf(buf, _field->format, _field->size, _field->value);
  299.             Highlight(_field, ON);
  300.             pos = 0;
  301.             break;
  302.  
  303.  
  304.  
  305.             /* home: go to the first character in the field */
  306.  
  307.          case HOME:
  308.             x -= pos;
  309.             pos = 0;
  310.             gotoxy(x, y);
  311.             break;
  312.  
  313.  
  314.             /* end: go to the last character in the field */
  315.  
  316.          case END:
  317.             x += _field->size - pos - 1;
  318.             pos = _field->size - 1;
  319.             gotoxy(x, y);
  320.             break;
  321.  
  322.  
  323.             /* page up: go to the first field */
  324.  
  325.          case PGUP:
  326.             Highlight(_field, OFF);
  327.             _field = field;
  328.             sprintf(buf, _field->format, _field->size, _field->value);
  329.             x = _field->row;
  330.             y = _field->col;
  331.             pos = 0;
  332.             Highlight(_field, ON);
  333.             break;
  334.  
  335.  
  336.             /* page down: go to the last field */
  337.  
  338.          case PGDN:
  339.             Highlight(_field, OFF);
  340.             _field = field + MAXFIELD - 1;
  341.             sprintf(buf, _field->format, _field->size, _field->value);
  342.             x = _field->row;
  343.             y = _field->col;
  344.             pos = 0;
  345.             Highlight(_field, ON);
  346.             break;
  347.  
  348.  
  349.             /* delete: delete the character at the cursor */
  350.  
  351.          case BS:
  352.          case LEFT:
  353.             if(pos)
  354.             {
  355.                pos--;
  356.                x--;
  357.                gotoxy(x, y);
  358.             }
  359.             else
  360.                break;
  361.  
  362.             if(!INSERT || key == LEFT)
  363.                break;
  364.  
  365.          case DEL:
  366.             delete(buf + pos);
  367.             cputs(buf + pos);
  368.             _field->value = atoi(buf);
  369.             break;
  370.  
  371.  
  372.             /* right arrow: goto the next space */
  373.  
  374.          case SPACE:
  375.          case RIGHT:
  376.             if(pos < _field->size - 1)
  377.             {
  378.                pos++;
  379.                x++;
  380.                gotoxy(x, y);
  381.             }
  382.             break;
  383.  
  384.  
  385.  
  386.             /* process numeric characters, spaces and minus signs */
  387.             /* (falls through)                                     */
  388.  
  389.          default:
  390.             if(isdigit((char)key)
  391.                || ((char)key == '-' && *(_field->format + 2) == 'd'))
  392.             {
  393.                x -= pos;
  394.                pos -= space(buf + pos);
  395.                x += pos;
  396.                gotoxy(x, y);
  397.  
  398.                if(INSERT)
  399.                   insert(key, buf + pos);
  400.                else
  401.                   *(buf + pos) = key;
  402.  
  403.                if(*(buf + pos + 1) == ' ')
  404.                   blank(buf + pos + 2);
  405.  
  406.                _field->value = atoi(buf);
  407.                cputs(buf + pos);
  408.  
  409.                if(pos < _field->size - 1)
  410.                {
  411.                   pos++;
  412.                   x++;
  413.                }
  414.                gotoxy(x, y);
  415.             }
  416.             break;
  417.  
  418.  
  419.  
  420.  
  421.             /* f1: load a tone into the sound queue */
  422.  
  423.          case F1:
  424.             SndTone((field + 0)->value, (field + 1)->value);
  425.             break;
  426.  
  427.  
  428.             /* f2: load a pause into the sound queue */
  429.  
  430.          case F2:
  431.             SndPause((field + 2)->value);
  432.             break;
  433.  
  434.  
  435.             /* f3: load a string of notes into the sound queue */
  436.  
  437.          case F3:
  438.             SndString(scale, 8, (field + 3)->value);
  439.             break;
  440.  
  441.  
  442.             /* f4: load a sliding tone into the sound queue */
  443.  
  444.          case F4:
  445.             SndSlide((field + 4)->value, (field + 5)->value,
  446.                      (field + 6)->value, (field + 7)->value);
  447.             break;
  448.  
  449.  
  450.             /* f5: load a trill tone into the sound queue */
  451.  
  452.          case F5:
  453.             SndTrill((field + 8)->value, (field + 9)->value,
  454.                 (field + 10)->value, (field + 11)->value, (field + 12)->value);
  455.             break;
  456.  
  457.  
  458.             /* f6: load a interval tone into the sound queue */
  459.  
  460.          case F6:
  461.             SndPulse((field + 13)->value, (field + 14)->value,
  462.                                     (field + 15)->value, (field + 16)->value);
  463.             break;
  464.  
  465.  
  466.             /* f7: flush the sound queue */
  467.  
  468.          case F7:
  469.             SndFlush();
  470.             break;
  471.  
  472.  
  473.          case F8:
  474.             tbase += 3;
  475.             tbase &= 3;
  476.             gotoxy(SNDRES);
  477.             textattr(VALUES);
  478.             cputs(*(tbase_txt + tbase));
  479.             textattr(FIELD);
  480.             SndTbase(tbase);
  481.             break;
  482.  
  483.          case F9:
  484.             SndHold(hold ^= 1);
  485.             gotoxy(SNDHOLD);
  486.             textattr(VALUES);
  487.             cputs(*(hold_txt + hold));
  488.             textattr(FIELD);
  489.             break;
  490.  
  491.          case F10:
  492.             SndTerm();
  493.             break;
  494.  
  495.             /* escape: quit snddemo */
  496.  
  497.          case ESC:
  498.             Uninit();
  499.             return(0);
  500.  
  501.       }
  502.    }
  503. }
  504.  
  505.  
  506.  
  507. /*
  508. //-------------------------------------
  509. //
  510. // Initialize
  511. //
  512. //-------------------------------------
  513. */
  514.  
  515. void Init(void)
  516. {
  517. int i = MAXFIELD;
  518. int row = TMAR;
  519. struct field *_field;
  520. FILE *ini;
  521. struct{
  522.  int x;
  523.  int y;
  524.  char *text;
  525.  }title[3] = {  {  5,  1, "Sound Generation"  },
  526.        { 56,  1, "Status"     },
  527.       { 56,  7, "Control"     }
  528.      };
  529.  
  530. char *function[] = {
  531.  
  532.    "F1 - SndTone  (     ,    )                              SndLen() =  ",
  533.    "F2 - SndPause (    )",
  534.    "F3 - SndString(scale,   8,    )                     F7  - SndFlush()",
  535.    "F4 - SndSlide (     ,    ,    ,    )                F8  - SndTbase(      )",
  536.    "F5 - SndTrill (     ,     ,    ,    ,    )          F9  - SndHold(   )",
  537.    "F6 - SndPulse (     ,    ,    ,    )                F10 - SndTerm() ",
  538.    "         SndMs(      ) =                            ESC- Quit"
  539.                   };
  540.  
  541.       /* Initialize sefx with a 1023 byte sound queue (341 tones)  */
  542.  
  543.    SndOpen(1023, 3);
  544.  
  545.  
  546.       /* Read the initialization file if one exists   */
  547.  
  548.    ini = fopen("snddemo.ini", "rb");
  549.    if(ini)
  550.    {
  551.       i = MAXFIELD;
  552.       while(i--)
  553.          (field + i)->value = fgetw(ini);
  554.       fclose(ini);
  555.    }
  556.  
  557.  
  558.       /* Clear the screen */
  559.  
  560.    textattr(TEXT);
  561.    clrscr();
  562.  
  563.  
  564.       /* Put the function names on the screen */
  565.  
  566.    i = 0;
  567.    while(i < 7)
  568.    {
  569.       gotoxy(COL, row);
  570.       row += VSPC;
  571.       cputs(*(function + i++));
  572.    }
  573.  
  574.  
  575.       /* Put the values on the screen and */
  576.       /* and highlight the first field    */
  577.  
  578.    textattr(VALUES);
  579.    gotoxy(SNDRES);
  580.    cputs(*(tbase_txt + tbase));
  581.    _field = field + 1;
  582.    for(i=1; i<MAXFIELD; i++)
  583.    {
  584.       gotoxy(_field->col, _field->row);
  585.       cprintf(_field->format, _field->size, _field->value);
  586.       _field++;
  587.    }
  588.    gotoxy(SNDHOLD);
  589.    textattr(VALUES);
  590.    cputs(*hold_txt);
  591.    textattr(FIELD);
  592.    Highlight(field, ON);
  593.  
  594.  
  595.  
  596.   /* Draw the borders */
  597.  
  598.  gotoxy(1, 1);
  599.  textattr(BORDER);
  600.    cputs(" ╒[                  ]══════════════════"
  601.          "════════════╤[        ]═══════════════╕" );
  602.    for(i=2; i<24; i++)
  603.    {
  604.   if(i == 7)
  605.   {
  606.    cputs("  │                                                "
  607.        "  ╞[         ]══════════════╡");
  608.    continue;
  609.   }
  610.       gotoxy(2, i);
  611.       putch('│');
  612.       gotoxy(53, i);
  613.       putch('│');
  614.       gotoxy(79, i);
  615.       putch('│');
  616.    }
  617.    cputs("  ╘══════════════════════════════════════"
  618.          "════════════╧═════════════════════════╛" );
  619.  
  620.  
  621.  textattr(TITLE);
  622.  for(i=0; i<3; i++)
  623.  {
  624.   gotoxy((title + i)->x, (title + i)->y);
  625.   cputs((title + i)->text);
  626.  }
  627.  
  628.  
  629.  
  630.       /* Put the footer on the screen */
  631.  
  632.    gotoxy(2,25);
  633.    textattr(INFO);
  634.    cputs("           Sefx version 1.0  -  Copyright (c) 1992 Bri Productions           ");
  635.  
  636.  
  637.       /* locate the cursor */
  638.  
  639.    gotoxy(x, y);
  640. }
  641.  
  642.  
  643.  
  644. /*
  645. //-------------------------------------
  646. //
  647. // Uninitialize
  648. //
  649. //-------------------------------------
  650. */
  651.  
  652. void Uninit(void)
  653. {
  654. int i = MAXFIELD;
  655. FILE *ini;
  656.  
  657.  
  658.       /* clear the screen */
  659.  
  660.    textattr(LIGHTGRAY);
  661.    clrscr();
  662.    SndClose();
  663.  
  664.  
  665.       /* write the values to the initialization file */
  666.  
  667.    ini = fopen("snddemo.ini", "wb");
  668.    if(ini)
  669.    {
  670.       while(i--)
  671.          fputw((field + i)->value, ini);
  672.       fclose(ini);
  673.    }
  674.  
  675.  
  676.       /* put the trailing message on the screen */
  677.  
  678.    puts("Sefx version 1.0  -  Copyright (c) 1992 Bri Productions");
  679.    cursor(SMALL);
  680. }
  681.  
  682.  
  683. /*
  684. //-------------------------------------
  685. //
  686. // highlight or un-highlight a field
  687. //
  688. //-------------------------------------
  689. */
  690.  
  691. void Highlight(struct field *_field, int on_off)
  692. {
  693. int attr[2] = { VALUES, FIELD };
  694.  
  695.  
  696.       /* Locate the cursor and set the text color */
  697.  
  698.    x = _field->col;
  699.    y = _field->row;
  700.    gotoxy(x, y);
  701.    textattr(*(attr + on_off));
  702.  
  703.  
  704.       /* reput the value in the new attribute */
  705.  
  706.    cprintf(_field->format, _field->size, _field->value);
  707.  
  708.  
  709.       /* restore the text attribute and cursor location  */
  710.  
  711.    textattr(FIELD);
  712.    gotoxy(x, y);
  713. }
  714.  
  715.  
  716. /*
  717. //-------------------------------------
  718. //
  719. // update milliseconds to tics
  720. //
  721. //-------------------------------------
  722. */
  723.  
  724. void UpdateMs(void)
  725. {
  726. int tics;
  727.  
  728.    tics = SndMs((field + 17)->value);
  729.    gotoxy(SNDMS);
  730.    cprintf("%-4u", tics);
  731.  
  732. }
  733.  
  734.  
  735. /*
  736. //-----------------------------------------------------------------------------
  737. //
  738. // insert
  739. //
  740. // inserts a character into a string
  741. //
  742. //-----------------------------------------------------------------------------
  743. */
  744.  
  745. void insert(char c, char *ins)
  746. {
  747. char temp;
  748.  
  749.    do
  750.    {
  751.       temp = *ins;
  752.       *ins++ = c;
  753.       c = temp;
  754.    }
  755.    while(*ins);
  756. }
  757.  
  758. /*
  759. //-----------------------------------------------------------------------------
  760. //
  761. // delete
  762. //
  763. // deletes a character from a string
  764. //
  765. //-----------------------------------------------------------------------------
  766. */
  767.  
  768. void delete(char *del)
  769. {
  770.    while(*(del + 1))
  771.       *del++ = *(del + 1);
  772.    *del = ' ';
  773. }
  774.  
  775.  
  776. /*
  777. //-----------------------------------------------------------------------------
  778. //
  779. // blank
  780. //
  781. // blanks a string
  782. //
  783. //-----------------------------------------------------------------------------
  784. */
  785.  
  786. void blank(char *blnk)
  787. {
  788.    while(*blnk)
  789.       *blnk++ = ' ';
  790. }
  791.  
  792.  
  793. /*
  794. //-----------------------------------------------------------------------------
  795. //
  796. // space
  797. //
  798. // counts the number of leading spaces
  799. //
  800. //-----------------------------------------------------------------------------
  801. */
  802.  
  803. int space(char *c)
  804. {
  805. int rv = 0;
  806.  
  807.    while(*--c == ' ')
  808.       rv++;
  809.    return(rv);
  810. }
  811.